import StateMachine from 'javascript-state-machine';

let fms = new StateMachine({
    init: '收藏',
    transitions: [{
            name: 'doStore',
            from: '收藏',
            to: '取消收藏'
        },
        {
            name: 'cancelStore',
            from: '取消收藏',
            to: '收藏'
        }
    ],
    methods: {
        //监听执行收藏
        onDoStore: function () {
            alert('收藏成功');
            updateText()
        },
        onCancelStore: function () {
            alert('取消收藏');
            updateText()
        }
    }
})

let btn = document.getElementById('button');

btn.onclick = function () {
    if (fms.is('收藏')) {
        fms.doStore()
    } else {
        fms.cancelStore()
    }
}

function updateText() {
    btn.innerHTML = fms.state;
}

updateText();